<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use app\admin\model\Config;
use app\util\Util;
use think\Db;

/*
 * 网站配置
 */
class ConfigController extends CommonController
{
    /*
     * 网站配置
     */
    public function index()
    {
        $row = Config::where('id', '=', 1)->find();

        return view('index', [
            'row' => $row,
        ]);
    }

    /*
     * 保存网站配置
     */
    public function postSave()
    {
        $all = input('post.');

        $data = [
            'title' => $all['title'],
            'keywords' => $all['keywords'],
            'description' => $all['description'],
            'copyright' => $all['copyright'],
            'beian' => $all['beian'],
            'intro' => $all['intro'],
        ];

        $result = Config::where('id', '=', 1)->update($data);
        if ($result) {
            return Util::ajaxReturn(1, '保存成功', null);
        } else {
            return Util::ajaxReturn(0, '保存失败', null);
        }
    }

    public function xmlToArray()
    {
        set_time_limit(300);

        $xmlFile = ROOT_PATH . 'public' . DS . 'LocList.xml';

        $xml = simplexml_load_file($xmlFile);

        foreach ($xml->children() as $country) {
            foreach ($country->children() as $state) {
                $attributesState = $state->attributes();
                $i = 0;
                foreach ($attributesState as $k => $v) {
                    $i++;
                    if ($i % 2 == 1) {
                        echo 'name:' . $v . '<br>';

                        $data = [
                            'name' => $v,
                            'level' => 1,
                            'pid' => 0,
                        ];
                        $insertId = Db::name('city')->insertGetId($data);

                        foreach ($state->children() as $city) {
                            $attributesCity = $city->attributes();
                            $j = 0;
                            foreach ($attributesCity as $kk => $vv) {
                                $j++;
                                if ($j % 2 == 1) {
                                    echo '-----name:' . $vv . '<br>';

                                    $dataCity = [
                                        'name' => $vv,
                                        'level' => 2,
                                        'pid' => $insertId,
                                    ];

                                    Db::name('city')->insert($dataCity);
                                }
                            }


                        }


                    }
                }
            }

        }

    }


}